﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrKeHoach : DevComponents.DotNetBar.Office2007Form
    {
        public FrKeHoach()
        {
            InitializeComponent();
        }
        private string MaKH1, TenKH1, KhuVuc1, MaSP1, GhiChu1,search1,child=" - ",ID,TenCT1;
        private double SoLuong_d;
        private void combo_load()
        {
            Data dta = new Data();
            FormProcess Frp = new FormProcess();
            string[] arr = new string[] { "KV1N","KV1X", "KV2XP", "KV3", "KV4", "KV5_DG", "KV5_NK" };
            string sql_load = @"select * from dbo.Load_cbSP";
            Frp.cb1(cb_MaSP, sql_load, 1, 0);
            Frp.cb1(cb_MaSPTinhKH, sql_load, 1, 0);
            for (int i = 0; i < arr.Length; i++)
            {
                cb_KhuVuc.Items.Add(arr[i].ToString());
               // cb_.Items.Add(arr[i].ToString());
            }
            
        }

        private void txt_SoLuong_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuong);
        }

        private void FrKeHoach_Load(object sender, EventArgs e)
        {
            combo_load();
        }
        private void save()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            MaKH1 = txt_MaKH.Text.TrimStart();
            if (MaKH1.Length < 5)
            {
                if (MaKH1.Length == 1)
                    MaKH1 = "0000" + MaKH1;
                if (MaKH1.Length == 2)
                    MaKH1 = "000" + MaKH1;
                if (MaKH1.Length == 3)
                    MaKH1 = "00" + MaKH1;
                if (MaKH1.Length == 4)
                    MaKH1 = "0" + MaKH1;
            }
            TenKH1 = txt_TenKH.Text;
            KhuVuc1 = cb_KhuVuc.Text.Trim();
            SoLuong_d = double.Parse(txt_SoLuong.Text);
            if (cb_MaSP.Text != "")
                MaSP1 = prs.substring_right(cb_MaSP.Text, child);
            else
                MaSP1 = "";
            GhiChu1 = txt_GhiChu.Text;
            cmd.CommandText = "select datepart(yyyy,getdate())";

            ID = MaKH1 + KhuVuc1+cmd.ExecuteScalar().ToString();
            //GhiChu1 = txt_GhiChu.Text;
            if (MaKH1.Length == 5)
            {
                if (KhuVuc1 == "" || MaSP1 == "")
                    MessageBox.Show("Nhập sai thông tin trong ComboBox");
                else
                {
                    string insert1 = @"INSERT INTO [dbo].[KeHoachSP]([ID],[MaKeHoach],[TenKeHoach],[MaSP],[SoLuongSP],[KhuVuc],[GhiChu])
                        VALUES(N'" + ID + "',N'" + MaKH1 + "',N'" + TenKH1 + "',N'" + MaSP1 + "'," + SoLuong_d + ",N'" + KhuVuc1 + "',N'" + GhiChu1 + "')";

                    try
                    {
                        cmd.CommandText = insert1;
                        cmd.ExecuteNonQuery();
                        if (rd_SP.Checked == true)
                        {
                            cmd.CommandText = @"insert into dbo.KH_TheoDoi(ID,MaCT,ID_KeHoach,SLKH,SLTheoDoi,Ngay)
                                    SELECT     dbo.KeHoachSP.ID+dbo.ChiTiet_KhuVuc.MaCT as ID,dbo.ChiTiet_KhuVuc.MaCT as MaCT,dbo.KeHoachSP.ID as ID_KeHoach, dbo.ChiTiet_KhuVuc.SLCT_QuyDoi * dbo.KeHoachSP.SoLuongSP AS SLKH,dbo.ChiTiet_KhuVuc.SLCT_QuyDoi * dbo.KeHoachSP.SoLuongSP as SLTheoDoi,getdate() as Ngay
                                    FROM         dbo.ChiTiet_KhuVuc INNER JOIN
                                    dbo.KeHoachSP ON dbo.ChiTiet_KhuVuc.MaSP = dbo.KeHoachSP.MaSP
                                    WHERE     (dbo.ChiTiet_KhuVuc.KhuVuc = '" + KhuVuc1 + "' and dbo.KeHoachSP.ID='" + ID + "')";
                        }
                        if (rd_ChiTiet.Checked == true)
                        {
                            if (cb_TenCT.Text != "")
                                TenCT1 = prs.substring_right(cb_TenCT.Text, child);
                            if (TenCT1 != "")
                            {
                                cmd.CommandText = @"insert into dbo.KH_TheoDoi(ID,MaCT,ID_KeHoach,SLKH,SLTheoDoi,Ngay)
                                    SELECT     dbo.KeHoachSP.ID+dbo.ChiTiet_KhuVuc.MaCT as ID,dbo.ChiTiet_KhuVuc.MaCT as MaCT,dbo.KeHoachSP.ID as ID_KeHoach, dbo.ChiTiet_KhuVuc.SLCT_QuyDoi * dbo.KeHoachSP.SoLuongSP AS SLKH,dbo.ChiTiet_KhuVuc.SLCT_QuyDoi * dbo.KeHoachSP.SoLuongSP as SLTheoDoi,getdate() as Ngay
                                    FROM         dbo.ChiTiet_KhuVuc INNER JOIN
                                    dbo.KeHoachSP ON dbo.ChiTiet_KhuVuc.MaSP = dbo.KeHoachSP.MaSP
                                    WHERE     (dbo.ChiTiet_KhuVuc.KhuVuc = '" + KhuVuc1 + "' and dbo.KeHoachSP.ID='" + ID + "' and dbo.ChiTiet_KhuVuc.MaCT='"+TenCT1+"')";
                            }
                            else
                                MessageBox.Show("Nhập sai tên chi tiết");
                        }
                        //MessageBox.Show(cmd.CommandText.ToString());
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Dữ liệu được lưu thành công");
                        prs.reset_txt(groupPanel1);
                        cb_MaSP.Text = "";

                    }
                    catch (Exception ex)
                    {
                        string s = "insert duplicate key";
                        //string FK = "FOREIGN KEY constraint";
                        string s1 = ex.Message.ToString();
                        if (s1.Contains(s))
                        { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                        else
                        {
                            MessageBox.Show(ex.Message);
                            MessageBox.Show("Đã xảy ra lỗi khi thêm dữ liệu, vui lòng kiểm tra lại");
                            cmd.CommandText = @"delete from dbo.KeHoachSP where ID='" + ID + "'";
                            cmd.ExecuteNonQuery();
                        }
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        Conn.Close();
                        cmd.Dispose();
                    }
                }
            }
            else
                MessageBox.Show("Kế Hoạch Không Dài Quá 5 Ký Tự");
            
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            save();
        }
        private void search()
        {
            try
            {
                Process prs = new Process();
                DataSet ds = new DataSet();
                if (txt_MaKH.Text != "")
                {
                    search1 = @"SELECT     dbo.KeHoachSP.ID,dbo.KeHoachSP.MaKeHoach, dbo.KeHoachSP.TenKeHoach, dbo.SanPham.MaSP, dbo.SanPham.TenSP, dbo.KeHoachSP.SoLuongSP, 
                      dbo.KeHoachSP.KhuVuc, dbo.KeHoachSP.GhiChu
                        FROM         dbo.KeHoachSP INNER JOIN
                      dbo.SanPham ON dbo.KeHoachSP.MaSP = dbo.SanPham.MaSP where MaKeHoach=N'" + txt_MaKH.Text + "'";
                    ds = prs.dts(search1);

                }
                else
                {
                    if (cb_MaSP.Text != "")
                    {
                        MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                        if (MaSP1 == "")
                            MessageBox.Show("Nhập sai mã SP");
                        else
                        {
                            search1 = @"SELECT     dbo.KeHoachSP.MaKeHoach, dbo.KeHoachSP.TenKeHoach, dbo.SanPham.MaSP, dbo.SanPham.TenSP, dbo.KeHoachSP.SoLuongSP, 
                        dbo.KeHoachSP.KhuVuc, dbo.KeHoachSP.GhiChu
                        FROM         dbo.KeHoachSP INNER JOIN
                        dbo.SanPham ON dbo.KeHoachSP.MaSP = dbo.SanPham.MaSP where KeHoachSP.MaSP=N'" + MaSP1 + "'";
                            ds = prs.dts(search1);
                        }
                    }
                }
                //MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0];
                    int j = dataGridView1.Rows.Count - 2;
                    Display(j);
                    lb_index.Text = j.ToString();
                }
                else
                    MessageBox.Show("Không có thông tin. Vui lòng kiểm tra lại thông tin tìm kiếm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Display(int i)
        {
            txt_MaKH.Text = dataGridView1.Rows[i].Cells["MaKeHoach"].Value.ToString();
            cb_MaSP.Text = dataGridView1.Rows[i].Cells["TenSP"].Value.ToString() +" - "+ dataGridView1.Rows[i].Cells["MaSP"].Value.ToString();
            txt_TenKH.Text = dataGridView1.Rows[i].Cells["TenKeHoach"].Value.ToString();
            txt_SoLuong.Text = dataGridView1.Rows[i].Cells["SoLuongSP"].Value.ToString();
            txt_GhiChu.Text = dataGridView1.Rows[i].Cells["GhiChu"].Value.ToString();
            cb_KhuVuc.Text = dataGridView1.Rows[i].Cells["KhuVuc"].Value.ToString();
            lb_ID.Text = dataGridView1.Rows[i].Cells["ID"].Value.ToString();
        }
        private void edit()
        {
            Process prs = new Process();
            
            
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand();
            SqlConnection Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandText = "select datepart(YYYY,GETDATE())";
            try
            {
                ID = txt_MaKH.Text + cb_KhuVuc.Text + cmd.ExecuteScalar().ToString();
                string select = @"select KH_Theodoi.ID,Load_CTSP_Mua.TenSP,Load_CTSP_Mua.TenChiTiet,KH_Theodoi.SLKH,KH_Theodoi.SLTheoDoi
                                from dbo.KH_Theodoi inner join Load_CTSP_Mua on Load_CTSP_Mua.MaChiTiet=KH_Theodoi.MaCT
                                where KH_Theodoi.ID_KeHoach ='" + ID + "'";

                ds = prs.dts(select);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView2.DataSource = ds.Tables[0];
                    tabControl1.SelectedTab = tabItem2;
                }
                else
                {
                    MessageBox.Show("Không có dữ liệu");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_search_Click(object sender, EventArgs e)
        {
            search();
            cb_KhuVuc.Enabled = false;
            bt_delete.Enabled = true;
            bt_edit.Enabled = true;
        }

        private void bt_first_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                Display(0);
                lb_index.Text = "0";
            }
        }

        private void bt_prev_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView1.Rows.Count - 2)
                    {
                        i++;
                        Display(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_last_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                int i = dataGridView1.Rows.Count - 2;
                Display(i);
                lb_index.Text = i.ToString();
            }
        }

        private void bt_edit_Click(object sender, EventArgs e)
        {
            edit();
            cb_KhuVuc.Enabled = true;
        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            Process prs = new Process();
            prs.reset_txt(groupPanel1);
            cb_KhuVuc.Text = "";
            cb_KhuVuc.Enabled = true;
            dataGridView1.Rows.Clear();
        }
        private void delete()
        {
            DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa Kế Hoạch " + txt_MaKH.Text + cb_KhuVuc.Text, "Thông Báo", MessageBoxButtons.OKCancel);
            if (d == DialogResult.OK)
            {
                try
                {
                    string datecheck = @"select datediff(dd,Ngay,getdate()) from dbo.KeHoachSP where ID='" + lb_ID.Text + "'";
                    string delete = @"delete from dbo.KeHoachSP where ID='" + lb_ID.Text + "'";
                    MessageBox.Show(datecheck);
                    Data dta = new Data();
                    SqlConnection Conn = dta.Connect();
                    Conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = Conn;
                    cmd.CommandText = datecheck;
                    int date_count = int.Parse(cmd.ExecuteScalar().ToString());
                    if (date_count <= 10)
                    {
                        cmd.CommandText = delete;
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Dữ liệu được xóa thành công");

                        lb_ID.Text = "0";
                    }
                    else
                    {
                        MessageBox.Show("Quá thời gian cho phép,Không được phép xóa");
                    }
                    Conn.Close();
                    cmd.Dispose();
                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        dataGridView1.Rows.RemoveAt(0);
                    }
                    dta.reset_txt(groupPanel1);
                    bt_delete.Enabled = false;
                    bt_edit.Enabled = false;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void FrKeHoach_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            delete();
        }

        private void cb_MaSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                if (cb_MaSP.Text != "")
                {
                    MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                    if (MaSP1 == "")
                        MessageBox.Show("Nhập sai mã SP");
                }
                if (rd_ChiTiet.Checked == true)
                {
                    FormProcess Frp = new FormProcess();

                    string sql_load = @"select MaChiTiet,TenChiTiet from dbo.ChiTietSP where MaSP='" + MaSP1 + "'";
                    Frp.cb1(cb_TenCT, sql_load, 1, 0);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       

        private void rd_ChiTiet_CheckedChanged(object sender, EventArgs e)
        {
            lb_CT.Visible = true;
            cb_TenCT.Visible = true;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            DataSet ds = new DataSet();
            Conn.Open();
            cmd.Connection = Conn;
            string UpdateKH = "";
            try
            {
                for (int i = dataGridView2.Rows.Count - 2; i >= 0; i--)
                {
                    UpdateKH = @"UPDATE [dbo].[KH_Theodoi]
                            SET [SLKH] =" + dataGridView2.Rows[i].Cells["SLKH"].Value.ToString() + ",[SLTheoDoi] =" + dataGridView2.Rows[i].Cells["SLTheoDoi"].Value.ToString() + " where ID='" + dataGridView2.Rows[i].Cells["ID"].Value.ToString() + "'";
                    dataGridView2.Rows.RemoveAt(i);
                    cmd.CommandText = UpdateKH;
                    cmd.ExecuteNonQuery();
                }
                MessageBox.Show("Chỉnh sửa thành công");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void TinhKH_ChiTietUuTien()
        {
            Process prs = new Process();
           
            DataSet ds=new DataSet();
            
            try
            {
                if (cb_MaSPTinhKH.Text == "")
                {
                    if (txt_SLLKH.Text == "")
                        MessageBox.Show("Nhập đầy đủ thông tin sản phẩm và số lượng");
                    else
                    {
                        if (double.Parse(txt_SLLKH.Text) < 10)
                        {
                            GhiChu1 = @"select dbo.Load_CTSP_Mua.TenChiTiet,dbo.Load_CTSP_Mua.TenSP,qry1.SLCT_QuyDoi as SoLuongCT,qry2.TonKho AS SLTon,qry3.SoLuong,qry1.SLCT_QuyDoi*qry1.SLKH*" + txt_SLLKH.Text + " as SLLP,qry1.SLCT_QuyDoi*qry1.SLKH*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0) as SLNhap";
                            GhiChu1 += @"        from 
                                    (select dbo.ChiTiet_KhuVuc.MaCT,ChiTiet_KhuVuc.SLCT_QuyDoi,Load_cbSP.SLKH from dbo.ChiTiet_KhuVuc inner join dbo.Load_cbSP on dbo.Load_cbSP.MaSP=ChiTiet_KhuVuc.MaSP and KhuVuc='KV1X') as qry1 left join ";
                            GhiChu1 += @"               (select dbo.KTG_TK.MaCT,TonKho from dbo.KTG_TK) as qry2 on qry1.MaCT=qry2.MaCT
                                    left join (select MaCT,sum(dbo.KH_Theodoi.SLTheoDoi) as SoLuong from dbo.KH_Theodoi where dbo.KH_Theodoi.ID_KeHoach like '%KV1X%'
                                    group by MaCT) as qry3 on qry1.MaCT=qry3.MaCT inner join dbo.Load_CTSP_Mua on dbo.Load_CTSP_Mua.MaChiTiet=qry1.MaCT where qry1.SLCT_QuyDoi*qry1.SLKH*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0)>0";
                        }
                        else
                        {
                            GhiChu1 = @"select dbo.Load_CTSP_Mua.TenChiTiet,dbo.Load_CTSP_Mua.TenSP,qry1.SLCT_QuyDoi as SoLuongCT,qry2.TonKho AS SLTon,qry3.SoLuong,qry1.SLCT_QuyDoi*" + txt_SLLKH.Text + " as SLLP,qry1.SLCT_QuyDoi*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0) as SLNhap";
                            GhiChu1 += @"        from 
                                    (select dbo.ChiTiet_KhuVuc.MaCT,ChiTiet_KhuVuc.SLCT_QuyDoi,Load_cbSP.SLKH from dbo.ChiTiet_KhuVuc inner join dbo.Load_cbSP on dbo.Load_cbSP.MaSP=ChiTiet_KhuVuc.MaSP and KhuVuc='KV1X') as qry1 left join ";
                            GhiChu1 += @"               (select dbo.KTG_TK.MaCT,TonKho from dbo.KTG_TK) as qry2 on qry1.MaCT=qry2.MaCT
                                    left join (select MaCT,sum(dbo.KH_Theodoi.SLTheoDoi) as SoLuong from dbo.KH_Theodoi where dbo.KH_Theodoi.ID_KeHoach like '%KV1X%'
                                    group by MaCT) as qry3 on qry1.MaCT=qry3.MaCT inner join dbo.Load_CTSP_Mua on dbo.Load_CTSP_Mua.MaChiTiet=qry1.MaCT where qry1.SLCT_QuyDoi*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0)>0";
                            
                        }
                    }
                }
                else
                {
                    MaSP1 = prs.substring_right(cb_MaSPTinhKH.Text, child);
                    if (MaSP1 == "")
                        MessageBox.Show("Nhập sai thông tin");
                    else
                    {
                        if (txt_SLLKH.Text == "")
                            MessageBox.Show("Nhập thiếu thông tin");
                        else
                        {
                            if (double.Parse(txt_SLLKH.Text) < 10)
                            {
                                GhiChu1 = @"select dbo.Load_CTSP_Mua.TenChiTiet,dbo.Load_CTSP_Mua.TenSP,qry1.SLCT_QuyDoi as SoLuongCT,qry2.TonKho AS SLTon,qry3.SoLuong,qry1.SLCT_QuyDoi*qry1.SLKH*" + txt_SLLKH.Text + " as SLLP,qry1.SLCT_QuyDoi*qry1.SLKH*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0) as SLNhap";
                                GhiChu1 += @"        from 
                                    (select dbo.ChiTiet_KhuVuc.MaCT,dbo.Load_cbSP.SLKH,ChiTiet_KhuVuc.SLCT_QuyDoi from dbo.ChiTiet_KhuVuc inner join dbo.Load_cbSP on dbo.Load_cbSP.MaSP=ChiTiet_KhuVuc.MaSP and KhuVuc='KV1X' and dbo.Load_cbSP.MaSP='" + MaSP1 + "') as qry1 left join ";
                                GhiChu1 += @"               (select dbo.KTG_TK.MaCT,TonKho from dbo.KTG_TK) as qry2 on qry1.MaCT=qry2.MaCT
                                    left join (select MaCT,sum(dbo.KH_Theodoi.SLTheoDoi) as SoLuong from dbo.KH_Theodoi where dbo.KH_Theodoi.ID_KeHoach like '%KV1X%'
                                    group by MaCT) as qry3 on qry1.MaCT=qry3.MaCT inner join dbo.Load_CTSP_Mua on dbo.Load_CTSP_Mua.MaChiTiet=qry1.MaCT where qry1.SLCT_QuyDoi*qry1.SLKH*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0)>0";
                            }
                            else
                            {
                                GhiChu1 = @"select dbo.Load_CTSP_Mua.TenChiTiet,dbo.Load_CTSP_Mua.TenSP,qry1.SLCT_QuyDoi as SoLuongCT,qry2.TonKho AS SLTon,qry3.SoLuong,qry1.SLCT_QuyDoi*" + txt_SLLKH.Text + " as SLLP,qry1.SLCT_QuyDoi*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0) as SLNhap";
                                GhiChu1 += @"        from 
                                    (select dbo.ChiTiet_KhuVuc.MaCT,ChiTiet_KhuVuc.SLCT_QuyDoi from dbo.ChiTiet_KhuVuc where KhuVuc='KV1X' and MaSP='" + MaSP1 + "') as qry1 left join ";
                                GhiChu1 += @"               (select dbo.KTG_TK.MaCT,TonKho from dbo.KTG_TK) as qry2 on qry1.MaCT=qry2.MaCT
                                    left join (select MaCT,sum(dbo.KH_Theodoi.SLTheoDoi) as SoLuong from dbo.KH_Theodoi where dbo.KH_Theodoi.ID_KeHoach like '%KV1X%'
                                    group by MaCT) as qry3 on qry1.MaCT=qry3.MaCT inner join dbo.Load_CTSP_Mua on dbo.Load_CTSP_Mua.MaChiTiet=qry1.MaCT where qry1.SLCT_QuyDoi*" + txt_SLLKH.Text + "+isnull(qry3.SoLuong,0)-isnull(qry2.TonKho,0)>0";
                            }
                        }

                    }
                }
                //MessageBox.Show(GhiChu1);
                ds = prs.dts(GhiChu1);
                Fr_Report FrR = new Fr_Report();
                MaSP1 = cb_MaSPTinhKH.Text;
                FrR.report_KV1_CTUT(ds);
                FrR.Show();
                ds.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                
            }
        }

        private void bt_tinh_Click(object sender, EventArgs e)
        {
            TinhKH_ChiTietUuTien();
        }
    }
}
